<?php
/**
 * created by zzy
 * date: 2017/10/20 18:00
 */

namespace App\Eloquent\Ygt;

class DepartmentUser extends DbEloquent{

    protected $table    = 'ygt_department_user';

    // 取部门下的员工列表
    public static function getListByDepartmentId( $departmentId ){

        $where              = [ 'department_id'=>$departmentId ];
        $result            = self::getList( $where );
        return $result;
    }
    public static function getInfoByUserId( $userId ){

        $where              = [ 'user_id'=>$userId ];
        $result             = self::getInfo( $where );
        return $result;
    }
    // 根据厂id和用户id取员工信息
    public static function getInfoByCompanyIdAndUserId($companyId,$userId)
    {
        $where              = ['company_id'=>$companyId,'user_id'=>$userId];
        $result             = self::getInfo($where);
        return $result;
    }
    // 取用户当前所在厂对应的员工的相关信息
    public static function getCurrentInfo( $userId ){

        $user               = User::getInfo( ['id'=>$userId] );
        $companyId         = 0;
        if( $user )
        {
            $companyId     = $user->company_id;
        }
        $where              = ['company_id'=>$companyId,'user_id'=>$userId];
        $collection         = self::getInfo( $where,'',['id','desc'] );
        if($collection)
        {
            $roleType       = 1;//1工厂角色,2客商,3供应商,4收品单位
            $privilegeId    = $collection->privilege_id;
            $saleArr        = config('ygt.sale_arr');//客商配置-privilege_id-110
            $sellerArr      = config('ygt.seller_arr');//供应商配置-privilege_id-102
            $wasteBuyerArr  = config('ygt.waste_buyer_arr');//收品单位配置-privilege_id-109
            if($companyId == 2)
            {
                switch ($privilegeId)
                {
                    case 110:
                        $roleType       = 2;
                        break;
                    case 102:
                        $roleType       = 3;
                        break;
                    case 109:
                        $roleType       = 4;
                        break;
                    default:
                }
            }
            $collection->role_type  = $roleType;
        }
        return $collection;
    }
    // 取角色下的员工列表包含角色名称
    public static function getListByPrivilegeId( $privilegeId ){

        $where              = [ 'privilege_id'=>$privilegeId ];
        if( is_array( $privilegeId ) ){
            $where              = [ 'privilege_id'=>['in',$privilegeId] ];
        }
        $join               =[
            [ 'table'=>'ygt_privilege as a', 'field_l'=>'a.id', 'field_c'=>'=', 'field_r'=>'ygt_department_user.privilege_id' ]
        ];
        $field              = 'ygt_department_user.user_id,ygt_department_user.truename,a.title as privilege_title';
        $result             = self::getList( $where, $field, '','',['a.id','asc'],'',$join );
        return $result;
    }
    /**
     * created by zzy
     * 通过user_id 取得其对应的权限
     * @param $userId
     * @return bool|mixed
     */
    public static function getPermissionList( $userId ){

        $info               = self::getInfoByUserId( $userId );
        $result             = false;
        if( $info ){
            $privilegeId       = $info->privilege_id;
            $result             = Privilege::getPermissionList( $privilegeId );
        }
        return $result;
    }

    /**
     * created by zzy
     * * 通过user_id 取得其对应权限表中 对应的type_id值
     * @param $userId
     * @param $type ygt_permission表中的type值 0默认 1工序 2产品
     * @param bool $is_str true时返回字符串, false返回数组
     * @return bool 失败时false 成功时 字符串'1,2,3'或者数组[1,2,3]
     */
    public static function getPermissionTypeIds( $userId, $type, $is_str=true ){

        $lists              = self::getPermissionList( $userId );
        $result             = [];
        if( $lists ) {
            if ($is_str) {
                $result = $lists->where('type', $type)->implode('type_id', ',');
            } else {
                $result = $lists->where('type', $type)->keyBy('type_id')->keys()->toArray();
            }
        }
        return $result;
    }

    /**
     * created by zzy
     * 通过权限id取得对应的员工列表
     * @param $permission_id
     * @return mixed
     */
    public static function getListByPermissionId( $permission_id ){

        $join               =[
            [ 'table'=>'ygt_privilege_permission as pp', 'field_l'=>'pp.privilege_id', 'field_c'=>'=', 'field_r'=>'ygt_department_user.privilege_id' ],
        ];
        $where              = [ 'pp.permission_id'=>$permission_id ];
        $fileds             = 'ygt_department_user.user_id';
        $orderby            = [ 'pp.privilege_id', 'desc' ];
        $result             = self::getList( $where, $fileds, '', '', $orderby, '', $join );
        return $result;
    }

    //取用户厂的列表(主要是因为有多厂情况)
     public static function getCompanyListByUserId( $userId )
     {
         $where              = ['user_id'=>$userId];
         $join               = [
             ['table'=>'ygt_company as a', 'field_l'=>'a.id', 'field_c'=>'=', 'field_r'=>'ygt_department_user.company_id'],
         ];
         $fields            = 'ygt_department_user.*,a.title as title';
         $depart_user_list  = self::getList($where,$fields,'','','','',$join)->toArray();
         $result            = [];
         foreach( $depart_user_list as $key=>$val ){
             $result[]       = [
                 'id'            =>$val['company_id'],
                 'title'         =>$val['title'],
                 'privilege_id'  =>$val['privilege_id'],
             ];
         }
         return $result;
     }

    public function privilege(){
        return $this->belongsTo('App\Eloquent\Ygt\Privilege');
    }

    public function ygtUser(){
        return $this->belongsTo('App\Eloquent\Ygt\User','user_id');
    }

    public static function getWithPrivilege($privilegeIds){
        return self::whereIn('privilege_id',$privilegeIds)->with(['privilege' => function($query){
            $query->select(['id','title']);
        },'ygtUser'=>function($query){
            $query->select('id','avatar');
        }])->select(['id','user_id','truename','mobile','department_id','privilege_id','is_leader','status'])->get();
    }
}
